home *** CD-ROM | disk | FTP | other *** search
- Path: news.tuwien.ac.at!news
- From: Joachim Fabini <jo@vmars.tuwien.ac.at>
- Newsgroups: comp.lang.c,comp.windows.x.motif
- Subject: [Q:] flex input from string?
- Date: Fri, 12 Jan 1996 14:22:44 +0100
- Organization: RTSystems
- Distribution: inet
- Message-ID: <30F660A4.41C6@vmars.tuwien.ac.at>
- NNTP-Posting-Host: tunix.vmars.tuwien.ac.at
- Mime-Version: 1.0
- Content-Type: text/plain; charset=us-ascii
- Content-Transfer-Encoding: 7bit
- X-Mailer: Mozilla 2.0b3 (X11; I; OSF1 V2.1 alpha)
-
- Missing a dedicated (f)lex newsgroup, I'm posting this
- question to this newsgroup(s), hoping that someone can help me.
-
- I wrote a X-Motif-App with some dialogs and input forms.
- Flex-bison is used to read input from a file and fill in the different
- input form's fields.
-
- My question: (f)lex reads by default from the FILE* yyin,
- defaulting to stdin. How can I read input from a _STRING_ ? Say, I get
- the text field's value and pass it as input to the scanner, that
- returns some token and I know exactly if the input was correct or not.
- It's nonsense to do the whole checking in C, that only
- introduces unnecessary redundancy (if I change the scanner/parser,
- I have to change also the checking routine).
-
- At the moment I have three solutions, but none of them seems
- to me to be the ultimate one:
- 1. Write string to temporary file and open file as input stream
- (Worst approach ever seen)
- 2. fork(), open two pipes, and the child process simply writes
- the values it gets from the first pipe to the second.
- I.e a fputs/fgets pair in the application. Solution involves
- overhead... and works _only_ on Unix-Systems.
- 3. I doubt if this really works (platform independent):
- Defining YY_INPUT (as described in the flex man page) to:
-
- #define YY_INPUT(buf, result, max_size)\
- if((!global_ptr) || (!global_ptr[0])) { \
- result = YY_NULL; \
- }\
- else { \
- strncpy(buf, global_ptr, max_size); \
- result = strlen(buf); \
- global_ptr += result; \
- }
-
- This is the solution I'm using for the moment, (reading from
- a globally available pointer) and this solution _does_ work.
-
- The reason why I'm in doubt: I don't know how a unput(c)
- or some other function should work with this approach (as
- there is no stream to put the character back to)... Maybe
- there are also some other problems I just ignore?
-
- If someone experienced the same problem, has done the job in a
- different manner or just wants to give his oppinion (solution #3
- should work/ doesn't work/ is junk) please re-post and/or send
- mail to jo@vmars.tuwien.ac.at
-
- Thanks in advance
-
- --Jo
- ******************************************************************
- Joachim Fabini
- jo@vmars.tuwien.ac.at
- e9027595@stud1.tuwien.ac.at
- ******************************************************************
-